$(function() {
//Declare variable
	var  ProductType= $( "#ProductType" ),
			ProductName = $( "#ProductName" ),
			Category = $( "#Category" ),
			MainUnit = $( "#MainUnit" ),
			Commission=$("#Commission"),
			ComsType=$("#CommissionType"),			
			SubUnit = $( "#SubUnit" ),
			Scale = $( "#Scale" ),
			Price = $( "#Price" ),	
			Cost_branch = $( "#Cost_branch" ),	
			IsPOS = $( "#IsPOS" ),	
			IsPremium=$("#IsPremium"),
			IsStock = $( "#IsStock" ),	
			Times = $( "#Times" ),	
			LimitNumber=$("#LimitNumber"),
			Amount=$("#Amount"),
			allFields = $( [] ).add(ProductName)
								.add(Price)	
								.add(Times)
								.add(LimitNumber)
								.add(Amount)
								.add(Cost_branch)
								,
			tips = $( ".validateTips" ),
			button_name="",
			url="a_service.php", 
			ID="",
			AddTitle="Create new Product",	
			EditTitle="Update Product",	
			ViewTitle="View Product",
			rowSelect="",
			row_sub_table_cnt=0,
			o_list={};
			
		showLoader();
		// Set Icon to button
		$("#btnAddProduct").button({
			icons:{
			 primary: "ui-icon-circle-plus"
			}
		});
		
		//*********************
		// Load data to combobox 
		//********************
		//get json group
		$.get(url, {"c":"c"},function(data){
 
			var obj=eval('('+data+')');	
			if (obj != null){
				$.each(obj, function(index, text) {
					$("#cboCategory,#Category").append(
							$('<option></option>').val(index).html(text)
						 );
				});
			}
		});
		$.get(url, {"c":"rp"},function(data){
 
			var obj=eval('('+data+')');	
			if (obj != null){
				$.each(obj, function(index, text) {
					$("#productlist").append(
							$('<option></option>').val(text).html(index)
						 );
				});
			}
		});
		// for auto complated
		$( "#productlist" ).combobox();
	 
		$("#cboCategory").append($('<option></option>').val(0).html('All'));
		
			//get json unit
		$.get(url, {"c":"u"},function(data){
			var obj=eval('('+data+')');	
			if (obj != null){
				$.each(obj, function(index, text) {
					$("#MainUnit,#SubUnit").append(
							$('<option></option>').val(index).html(text)
						 );
				});
			}
		});
		$("#SubUnit").append($('<option></option>').val(0).html('--'));
		
		$.get(url, {"c":"t"},function(data){
			var obj=eval('('+data+')');	
			if (obj != null){		
				$.each(obj, function(index, text) {
					$("#cboType,#ProductType").append(
							$('<option></option>').val(index).html(text)
					);
				});
			}
		});
		$("#cboType").append($('<option></option>').val(0).html('All'));
		
		//********************* END ******************
		//*********************************************
		
			
		//Load First page
		LoadData(1,true);
		
		//Show Loading bar			
		function showLoader(){
			$( "#resultData tbody:first" ).prepend( "<tr>"
						+"<td colspan='7' class='search-background'>"
						+"<label><img src='./images/ajax-loader.gif'/></label>"
						+"</td>"
						+"</tr>" ); 
			 $('.search-background').fadeIn(900,0);
		}
		//Hide Loading bar			
		function hideLoader(){
			$('.search-background').fadeOut(300);
			$( "#resultData tbody tr" ).remove();
		};	
		
		function updateTips( t ) {
			tips
				.text( t )
				.addClass( "ui-state-highlight" );
			setTimeout(function() {
				tips.removeClass( "ui-state-highlight", 1500 );
			}, 500 );

		}

		function checkInputFile( o, n, min ) {
			if (o.val().length < min ) {
				o.addClass( "ui-state-error" );
				updateTips( "Please input " + n + "." );
				return false;
			} else {
				return true;
			}
		}
		
		function checkLength( o, n, min, max ) {
			if ( o.val().length > max || o.val().length < min ) {
				o.addClass( "ui-state-error" );
				updateTips( "Length of " + n + " must be between " +
					min + " and " + max + "." );
				return false;
			} else {
				return true;
			}
		}

		function checkRegexp( o, regexp, n ) {
			if ( !( regexp.test( o.val() ) ) ) {
				o.addClass( "ui-state-error" );
				updateTips( n );
				return false;
			} else {
				return true;
			}
		}
					
		function getData(id){
			var dataSet={'id':id,'m':'detail'};  
			 $.ajax({
				type: 'POST',
				cache: false,
				url: url,
				data: dataSet,
				success: function(data){
				
					var d=eval('('+data+')');
					var obj= eval('('+d['main']+')');
					var obj_detail=d['detail'];
					//clear array
					o_list={};
					//remove all rows;
					$( "#tbNewProduct tbody tr" ).remove();
					$("#productlist").find('option').remove();	
					// type=product
					if (obj["Type"]=="1"){
								
						for (value in obj){
							txt="#"+value;

							$(txt).val(obj[value]);
							 
							if (value =='IsStock'){
								$("#IsStock").attr('checked',obj[value]=='1'? true:false);
							}
							if (value =='IsPOS'){
								$("#IsPOS").attr('checked',obj[value]=='1'? true:false);
							}
							if (value =='IsPremium'){
								$("#IsPremium").attr('checked',obj[value]=='1'? true:false);
							}
						}
						$( "#dialog-form" ).dialog( "option", "height", 580 );
						$('fieldset').hide();
						$('.pos_hide').show();
						$('.stock_hide').show();
						$('.product_cost').show();
						$('.subUnit_hide').show();
						$('.premium_hide').show();
						$('.LimitAmount').show();
						$('.Times_hide').hide();
						$('.cost_on_branch').show();
						$('#lblGroup').html('Please select Product');
						$('#lblTitle').attr('innerHTML', 'Product: ');

						$.get(url, {"c":"rp"},function(data){
							var data=eval("("+data+")");
							$.each(data, function(index, text) {
								$("#productlist").append(
										$('<option></option>').val(text).html(index)
									);
								});
						});
						
					}else{ //type="treatment , set , course
						$( "#dialog-form" ).dialog( "option", "height",600 );				
						$('fieldset').show();
						$('.subUnit_hide').hide();
						$('.stock_hide').hide();
						$('.premium_hide').hide();
						$('.LimitAmount').hide();
						$('.product_cost').hide();
						$('.cost_on_branch').hide();
						$('.pos_hide').hide();
						for (value in obj){
							txt="#"+value;
 
							$(txt).val(obj[value]);
						}
						if (obj["Type"]=="4"){
							
							$('.Times_hide').show();
							$('#Amount').hide();
							$('#spanAmt').hide();
							
							$('#lblGroup').html('Please select Treatment & Set');
							$('#lblTitle').attr('innerHTML', 'List:');
							$.get(url, {"c":"ts"},function(data){
								var data=eval("("+data+")");
								$.each(data, function(index, text) {
									$("#productlist").append(
											$('<option></option>').val(text).html(index)
										);
									});
							});
						}else{
							$('#Amount').show();
							$('#spanAmt').show();
							$('.Times_hide').hide();
							$('.product_cost').hide();
							$('#lblGroup').html('Please select Product');
							$('#lblTitle').attr('innerHTML', 'Product: ');
							 
							$.get(url, {"c":"rp"},function(data){
								var data=eval("("+data+")");
								$.each(data, function(index, text) {
									$("#productlist").append(
											$('<option></option>').val(text).html(index)
										);
									});
							});
						}
						
						//$("#productlist").find('option').remove();
						
						if (!(obj_detail =="")){
							$( "#tbNewProduct tbody" ).prepend(obj_detail);
							
							$( "#tbNewProduct tbody tr").each(function(){
								// receive id detail
								var pid = $(this).find("td").eq(0).html();    
								// receive amount
								var amt = $(this).find("td").eq(2).html();
								
								o_list[pid]=amt;
							});
						}
						
					}
					
					//Set No. rows
					row_sub_table_cnt = obj['rows'];
					$("#iCount").attr('innerHTML',row_sub_table_cnt);
					
				}
			});
		}
		
		function callAjax(obj,id){
			var iRows=0;
			var m=$("#mode").val();
			
			iRows=$("#rowCount").attr('innerHTML');
			
			if (m=='edit'){
				iRows=rowSelect.find("td").eq(0).html();
			}
			
			
			$.ajax({
				type: 'POST',
				cache: false,
				url: url,
				data: {"m":m,"var":obj,"id":id,"rows":iRows},
				success: function(data){
					if (m=='add'){
					
						if( iRows==0 ){
							$( "#resultData tbody tr").remove();
						}
						//Insert data to first row.
						$( "#resultData tbody:first" ).prepend( data ); 
						//Set font color
						$( "#resultData tbody tr:first" ).css('color','blue');
						
						$("#rowCount").attr('innerHTML',parseInt(iRows,10)+1);
						
					}else if (m=='edit'){
						rowSelect.html(data);
					}
					// else if (m=='del'){
					// alert(data);
					// }
				}
			});
		}
		
		$( "#dialog:ui-dialog" ).dialog( "destroy" );
		
		//Confirm Dialog
		$( "#dialog-confirm" ).dialog({
			resizable: false,
			height:160,
			autoOpen: false,
			modal: true
		});
		
		//Form Edit Add 
		$( "#dialog-form" ).dialog({
			autoOpen: false,
			resizable:false,
			height: 500,
			width: 500,
			zIndex:9999,
			modal: true,
			open: function() { 
				if ($("#mode").val()=="add"){
					$('fieldset').hide();
					$('.stock_hide').show();
					$('.product_cost').hide();
					$('.premium_hide').show();
					$('.subUnit_hide').show();
					$('.cost_on_branch').show();
					$('.pos_hide').show();
					$('.Times_hide').hide();
					$('.LimitAmount').show();
					$('#lblGroup').html('Please select product');
					allFields.val( "" ).removeClass( "ui-state-error" );
					
				}else if ($("#mode" ).val() =="edit" || $("#mode" ).val()=="view"){
					allFields.val( "" ).removeClass( "ui-state-error" );
				}else{
					allFields.val( "" );
					$('fieldset').hide();
				}
			},
			close: function() {
				allFields.attr("readonly","");
				allFields.val( "" ).removeClass( "ui-state-error" );
			}
		});
		// Set value from control to array 
		function getValue(){
			var o = {};
			if ($("#ProductType option:selected").val()=="1"){
				o['Type']=$("#ProductType option:selected").val();
				o['ProductName']=ProductName.val();
				o['Category']=Category.val();
				o['MainUnit']=MainUnit.val();
				o['Commission']=Commission.val();
				o['CommissionType']=ComsType.val();
				o['SubUnit']=SubUnit.val();
				o['Scale']=Scale.val()==''?0:Scale.val();
				o['Price']=Price.val()==''?0:Price.val();
				o['Cost_branch']=Cost_branch.val()==''?0:Cost_branch.val();
				o['IsStock']=IsStock.attr('checked')==true? 1:0;
				o['IsPOS']=IsPOS.attr('checked')==true?1:0;
				o['IsPremium']=IsPremium.attr('checked')==true? 1:0;
				o['LimitNumber']=$("#LimitNumber").val()==""? 0:$("#LimitNumber").val();
			}else if (($("#ProductType option:selected").val()=="2") || ($("#ProductType option:selected").val()=="3")){
				o['Type']=$("#ProductType option:selected").val();
				o['ProductName']=ProductName.val();
				o['Category']=Category.val();
				o['MainUnit']=MainUnit.val();
				o['SubUnit']=SubUnit.val();
				o['Commission']=Commission.val();
				o['CommissionType']=ComsType.val();
				o['Scale']=Scale.val()==''?0:Scale.val();
				o['Price']=Price.val()==''?0:Price.val();
				o['Detail']=o_list;
				 
			}else if ($("#ProductType option:selected").val()=="4") {
				o['Type']=$("#ProductType option:selected").val();
				o['ProductName']=ProductName.val();
				o['Category']=Category.val();
				o['MainUnit']=MainUnit.val();
				o['SubUnit']=SubUnit.val();
				o['Commission']=Commission.val();
				o['CommissionType']=ComsType.val();
				o['Scale']=Scale.val()==''?0:Scale.val();
				o['Price']=Price.val()==''?0:Price.val();
				o['Detail']=o_list;
				o['Times']=Times.val();
			}
			return o;
		}
		function countArray(obj) {
		  var prop;
		  var propCount = 0;

		  for (prop in obj) {
			propCount++;
		  }
		  return propCount;
		}
		// Check input data in control
		function checkRequireControl(){
			var bValid=true;
			//check product Type 
			if ($("#ProductType option:selected").val()=="1"){
				bValid = bValid && checkInputFile( ProductName, "Product Name",1);
				if ($("#IsStock").attr('checked')){
					bValid = bValid && checkInputFile( Price, "Price",1);
				}
			}else if (($("#ProductType option:selected").val()=="2" )|| ($("#ProductType option:selected").val()=="3" ) ){
				bValid = bValid && checkInputFile( ProductName, "Product Name",1);
				bValid = bValid && checkInputFile( Price, "Price",1);
				
				//bValid = bValid && checkInputFile( $("#Amount"), "Amount",1);
				if (countArray(o_list)==0){
					updateTips( "Please selected project" );
					bValid = false;
				}
			}else if ($("#ProductType option:selected").val()=="4" ){
				bValid = bValid && checkInputFile( ProductName, "Product Name",1);
				bValid = bValid && checkInputFile( Price, "Price",1);
				bValid = bValid && checkInputFile( Times, "Times",1);
			}
			// bValid = bValid && checkInputFile( FirstName, "First Name",1);
			// bValid = bValid && checkInputFile( LastName, "Last Name",1);
			return bValid;
		}
		
		//Add Button
		$( "#btnAdd" ).live('click',function() {
				//Clear//
				o_list={};
				$("#Amount").val('0');
				ProductType.val('1');
				SubUnit.val('0');
				Scale.val('');
				
				$("#IsPOS").attr('checked',true);
				$("#IsStock").attr('checked',true);
				$("#IsPremium").attr('checked',false);
				
				$( "#tbNewProduct tbody tr" ).remove();
				//set mode
				$("#mode").val("add");
				//set title
				$("#dialog-form").dialog({'title':AddTitle});
				$("#dialog-form").dialog( "open" );
				$("#dialog-form").dialog( "option","buttons",{
					"Create":function(){
						allFields.removeClass( "ui-state-error" );
						var c=0;
						if ( checkRequireControl() ) {
							callAjax(getValue(),0); // call ajax function for add new data
							$( this ).dialog( "close" );
						}
					},
					"Cancel":function(){$( this ).dialog( "close" );}
				});
				
				$( "#dialog-form" ).dialog( "option", "height", 540 );
				$('fieldset').hide();
				$('.stock_hide').show();
			});
			
		$(".btnEdit").live('click',function() {
		 
			$("#mode").val("edit"),
			rowSelect=$(this).parent().parent(),
			ID=$(this).parent().parent().find("td").eq(1).html(),
	 
			getData(ID),
			$("#dialog-form").dialog({'title':EditTitle});
			$("#dialog-form").dialog("open");
			$("#dialog-form").dialog( "option","buttons",{
				"Update":function(){
					var bValid = true;
					allFields.removeClass( "ui-state-error" );
					if ( checkRequireControl() ) {
						
						callAjax(getValue(),ID); // call ajax for update 
						$( this ).dialog( "close" );
					}
				}
				,"Cancel":function(){
					$( this ).dialog( "close" );
				}
			}
			);
		});
		$(".btnDel").live('click',function(){
				rowSelect = $(this).parent().parent();
				ID=$(this).parent().parent().find("td").eq(1).html();
				//alert(ID);
				var tableRow  = $(this).parent().parent().parent();
				var irow=$(this).parent().parent().find("td").eq(0).html();
				//alert(ID);
				$("#mode").val("del");
				$("#dialog-confirm").dialog("open");
				$("#dialog-confirm").dialog("option","buttons",	{
					"Delete": function() {
						callAjax({"id":ID},0); 	//Call Ajax delete data
						rowSelect.remove(); // remove row
						//Remove number
						tableRow.find("tr").each(function(){
						if (irow < parseInt($(this).find("td").eq(0).html()))
							$(this).find("td").eq(0).html( parseInt($(this).find("td").eq(0).html()) - 1 );	
						});	
						
						var rows=$("#rowCount").attr('innerHTML');
						$("#rowCount").attr('innerHTML',parseInt(rows,10)-1);
						
						$( this ).dialog( "close" );
					},
					Cancel: function() {
							$( this ).dialog( "close" );
					}
				});
		});
		
		$(".btnView").live('click', function(){
			$("#mode").val("view");
			ID=$(this).parent().parent().find("td").eq(0).html();
			getData(ID); // get detail data 
			allFields.val( "" ).removeClass( "ui-state-error" ),
			$( ".validateTips" ).html(''),
			$("#dialog-form").dialog({'title':ViewTitle}),
			$("#dialog-form").dialog("open"),
			$("#dialog-form").dialog("option","buttons",{
				"Close":function(){
					allFields.attr("readonly","");
					$( this ).dialog( "close" );
					}
				});
		});
	
		//******************************************
		//*				 function popup
		//******************************************
		$("#ProductType").live('change',function(){
			$( ".ui-autocomplete-input" ).val('');
			if ($("#ProductType option:selected").val()=="1"){
				$( "#dialog-form" ).dialog( "option", "height", 580 ).fadeIn("slow");
			    $('fieldset').hide();
				$('.stock_hide').show();
				$('.cost_on_branch').show();
				$('.premium_hide').show();
				$('.subUnit_hide').show();
				$('.pos_hide').show();
				$('.Times_hide').hide();
				$('.LimitAmount').show();
				$('#lblGroup').html('Please select product');
				 
				if ($("#mode").val()=='add'){
					$('.product_cost').hide();
				}else{
					$('.product_cost').show();
				}
			}else {
				$( "#dialog-form" ).dialog( "option", "height",600 ).fadeIn("slow");
				$('fieldset').show();
				$('#Amount').show();
				$('#spanAmt').show();
				$('.pos_hide').hide();
				$('.stock_hide').hide();
				$('.premium_hide').hide();
				$('.subUnit_hide').hide();
				$('.LimitAmount').hide();
				$('.cost_on_branch').hide();
				$('.product_cost').hide();
				$("#productlist").find('option').remove();		

				$("#productlist").val('');
				
				// Treatment
				if ($("#ProductType option:selected").val()=="2"){
					$('.Times_hide').hide();
					$('#lblGroup').html('Please select Product');
					$('#lblTitle').attr('innerHTML', 'Product: ');
					
					$.get(url, {"c":"rp"},function(data){
						var data=eval("("+data+")");
						$.each(data, function(index, text) {
							$("#productlist").append(
									$('<option></option>').val(text).html(index)
								);
							});
					});
				//set 
				}else if ($("#ProductType option:selected").val()=="3"){
					$('.Times_hide').hide();
					$('#lblGroup').html('Please select Product');
					$('#lblTitle').attr('innerHTML', 'Product:');
					$.get(url, {"c":"rp"},function(data){
						var data=eval("("+data+")");
						$.each(data, function(index, text) {
							$("#productlist").append(
									$('<option></option>').val(text).html(index)
								);
							});
					});
				//Course
				}else if ($("#ProductType option:selected").val()=="4"){
					$('.Times_hide').show();
					$('#Amount').hide();
					$('#spanAmt').hide();
					
					$('#lblGroup').html('Please select Treatment & Set');
					$('#lblTitle').attr('innerHTML', 'List:');
					$.get(url, {"c":"ts"},function(data){
						var data=eval("("+data+")");
						$.each(data, function(index, text) {
							$("#productlist").append(
									$('<option></option>').val(text).html(index)
								);
							});
					});
				}
			}
		});
	
		$("#btnRemove").live('click',function(){
			var subid;
			rowSelect = $(this).parent().parent();
			subid=$(this).parent().parent().find("td").eq(0).html();
			delete o_list[subid];// Remove data
			row_sub_table_cnt--;
			$("#iCount").attr('innerHTML',row_sub_table_cnt);
			rowSelect.remove();
		});
	
		$("#btnAddProduct").live('click',function(){
			var amt=0;
			//Course
			if ($("#ProductType option:selected").val()=="4"){
				amt=1;
			}else {
				amt=$("#Amount").val();
			}
			
			if ((amt =='0' )|| (amt=='')){
				alert('Please input amount');
			}else{

				if (o_list[$("#productlist").val()]) {
					alert($("#productlist option:selected").text()+ ' already exist!!');
				}else{
				//	alert('d');
					o_list[$("#productlist").val()]= amt || '';
					//set rows no.
					row_sub_table_cnt++;						
					$("#iCount").attr('innerHTML',row_sub_table_cnt);
						 					
					$( "#tbNewProduct tbody:first" ).prepend( 
						"<tr>"
							+"<td align='right'>"+$("#productlist").val()+"</td>"
							+"<td align='left'>"+$("#productlist option:selected").text()+"</td>"
							+"<td align='right'>"+amt+"</td>"
							+"<td align='center'><img src='images/remove16.png' style='cursor:pointer' id='btnRemove' border=0/></td>"
						+"</tr>"
					);
				}
			}
			return false;
		});
			
		$("#btnSearch").click(function(){
			LoadData(1,true);
			
		});
	$("#paging_button li").live('click',function(){
		$("#paging_button li").css({'background-color' : ''});
 		LoadData(this.id,false);

	});
	//loading 
		function LoadData(page,b){
			$( "#resultData tbody tr" ).remove();
			//Show loading
			showLoader();
			//set criteria
			var dataSet={'m':'search'
						,'sname':$("#txtServiceName_s").val()	
						,'ename':$("#txtEmployee_s").val()
						,'page':page
						};
			 //Post 		
			 $.post(url,  dataSet , function(data){
				// alert(data);
				var obj=eval("(" + data + ")");

				//hide loading
				hideLoader();
				
				//set data to table
				$( "#resultData tbody" ).append(obj['data']);
				//set row 
				$("#rowCount").attr('innerHTML',obj['rows']);
				//set page
				if (b){
 
					if (parseInt(obj['totalpage'])>1){
						$("#paging_btn").show();
						setPaging(parseInt(obj['totalpage']));
					}else{
						$("#paging_btn").hide();
					}						 
				}
 
			 });
		}

		function setPaging(iTotal){
				 $("#paging_btn").paginate({
				count 		: iTotal,
				start 		: 1,
				display     : 10,
				border					: false,
				text_color  			: '#888',
				background_color    	: '#EEE',	
				text_hover_color  		: 'blue',
				background_hover_color	: '#CFCFCF',
				mouse					: 'press',
				onChange     			: function(page){
											LoadData(page,false);
										  }
			});
		}
		
	});
(function( $ ) {
		$.widget( "ui.combobox", {
			_create: function() {
				var self = this,
					select = this.element.hide(),
					selected = select.children( ":selected" ),
					value = selected.val() ? selected.text() : "";
				var input = this.input = $( "<input>" )
					.insertAfter( select )
					.val( value )
					.autocomplete({
						delay: 0,
						minLength: 0,
						source: function( request, response ) {
							var matcher = new RegExp( $.ui.autocomplete.escapeRegex(request.term), "i" );
							response( select.children( "option" ).map(function() {
								var text = $( this ).text();
								if ( this.value && ( !request.term || matcher.test(text) ) )
									return {
										label: text.replace(
											new RegExp(
												"(?![^&;]+;)(?!<[^<>]*)(" +
												$.ui.autocomplete.escapeRegex(request.term) +
												")(?![^<>]*>)(?![^&;]+;)", "gi"
											), "<strong>$1</strong>" ),
										value: text,
										option: this
									};
							}) );
						},select: function( event, ui ) {
							ui.item.option.selected = true;
							self._trigger( "selected", event, {
								item: ui.item.option
							});
						},
						change: function( event, ui ) {
							if ( !ui.item ) {
								var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( $(this).val() ) + "$", "i" ),
									valid = false;
								select.children( "option" ).each(function() {
									if ( $( this ).text().match( matcher ) ) {
										this.selected = valid = true;
										return false;
									}
								});
								if ( !valid ) {
									// remove invalid value, as it didn't match anything
									$( this ).val( "" );
									select.val( "" );
									input.data( "autocomplete" ).term = "";
									return false;
								}
							}
						}
					})
					.addClass( "ui-widget ui-widget-content ui-corner-left" );
					input.data( "autocomplete" )._renderItem = function( ul, item ) {
					return $( "<li></li>" )
						.data( "item.autocomplete", item )
						.append( "<a>" + item.label + "</a>" )
						.appendTo( ul );
				};

				this.button = $( "<button type='button'>&nbsp;</button>" )
					.attr( "tabIndex", -1 )
					.attr( "title", "Show All Items" )
					.insertAfter( input )
					.button({
						icons: {
							primary: "ui-icon-triangle-1-s"
						},
						text: false
					})
					.removeClass( "ui-corner-all" )
					.addClass( "ui-corner-right ui-button-icon" )
					.click(function() {
						// close if already visible
						if ( input.autocomplete( "widget" ).is( ":visible" ) ) {
							input.autocomplete( "close" );
							return;
						}

						// pass empty string as value to search for, displaying all results
						input.autocomplete( "search", "" );
						input.focus();
					});
			},

			destroy: function() {
				this.input.remove();
				this.button.remove();
				this.element.show();
				$.Widget.prototype.destroy.call( this );
			}
		});
})( jQuery );
	